package org.jeecg.modules.hc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.hc.entity.HcMerchantCommission;
import org.jeecg.modules.hc.entity.HcSiteAddressDetails;
import org.jeecg.modules.hc.entity.HcSiteRelation;
import org.jeecg.modules.hc.mapper.HcMerchantCommissionMapper;
import org.jeecg.modules.hc.mapper.HcSiteRelationMapper;
import org.jeecg.modules.hc.service.IHcMerchantCommissionService;
import org.jeecg.modules.hc.service.IHcSiteAddressDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 门店抽佣
 * @Author: jeecg-boot
 * @Date: 2023-02-01
 * @Version: V1.0
 */
@Service
public class HcMerchantCommissionServiceImpl extends ServiceImpl<HcMerchantCommissionMapper, HcMerchantCommission> implements IHcMerchantCommissionService {

    @Autowired
    private IHcSiteAddressDetailsService hcSiteAddressDetailsService;

    @Autowired
    private HcSiteRelationMapper hcSiteRelationMapper;

    public BigDecimal getCommission(HcMerchantCommission hcMerchantCommission, BigDecimal price) {
        BigDecimal result = new BigDecimal(0);

        if (hcMerchantCommission != null) {
            if (hcMerchantCommission.getAmount() != null) {
                result = hcMerchantCommission.getAmount();
            } else if (hcMerchantCommission.getPercentage() != null) {
                result = price.divide(new BigDecimal("100")).setScale(2, RoundingMode.HALF_UP).multiply(hcMerchantCommission.getPercentage());
            }
        }

        return result;
    }

    public void edit(HcSiteAddressDetails param) {
        for (HcMerchantCommission hcMerchantCommission : param.getHcMerchantCommissionList()) {
            if (StringUtils.isNotBlank(hcMerchantCommission.getId())) {
                this.update(new LambdaUpdateWrapper<HcMerchantCommission>()
                        .set(HcMerchantCommission::getAmount, hcMerchantCommission.getAmount())
                        .set(HcMerchantCommission::getPercentage, hcMerchantCommission.getPercentage())
                        .eq(HcMerchantCommission::getId, hcMerchantCommission.getId())
                );
            } else {
                this.save(hcMerchantCommission);
            }
        }
    }

    public IPage<HcSiteAddressDetails> pageList(Page<HcSiteAddressDetails> page) {
        Page<HcSiteAddressDetails> pageList = hcSiteAddressDetailsService.page(page);

        for (HcSiteAddressDetails record : pageList.getRecords()) {
            List<HcSiteRelation> hcSiteRelationList = hcSiteRelationMapper.getListBySiteId(record.getSiteId());

            List<HcMerchantCommission> hcMerchantCommissionList = new ArrayList<>();

            for (HcSiteRelation hcSiteRelation : hcSiteRelationList) {
                HcMerchantCommission hcMerchantCommission = this.getOne(new LambdaQueryWrapper<HcMerchantCommission>()
                        .eq(HcMerchantCommission::getAddressId, record.getId())
                        .eq(HcMerchantCommission::getMerchantId, hcSiteRelation.getMerchantId())
                        .last(" limit 1 ")
                );

                if (hcMerchantCommission == null) {
                    hcMerchantCommission = new HcMerchantCommission();
                    hcMerchantCommission.setAddressId(record.getId());
                    hcMerchantCommission.setMerchantId(hcSiteRelation.getMerchantId());
                }

                hcMerchantCommission.setMerchantName(hcSiteRelation.getMerchantName());
                hcMerchantCommissionList.add(hcMerchantCommission);
            }

            record.setHcMerchantCommissionList(hcMerchantCommissionList);
        }

        return pageList;
    }
}
